package com.magic.smartcity.view.more.adapters

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.get
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.navigation.Navigation
import androidx.recyclerview.widget.RecyclerView
import com.magic.smartcity.R
import com.magic.smartcity.beans.MetroCurrentStationLines
import com.magic.smartcity.beans.MetroLine
import com.magic.smartcity.databinding.ItemMetroLineBinding
import com.magic.smartcity.view.more.adapters.MetroHomeLinesAdapter.ItemHolder

/**
 * @description:
 * @create: Create by Magic on 2022/2/14.
 */
class MetroHomeLinesAdapter : RecyclerView.Adapter<MetroHomeLinesAdapter.ItemHolder>() {

    //所有线路
    private val list: MutableList<MetroCurrentStationLines> = mutableListOf()
    private var mNowLocation:String = ""

    fun setData(nowLocation:String,list: List<MetroCurrentStationLines>) = this.list.apply {
        clear()
        addAll(list)
        mNowLocation = nowLocation
        notifyDataSetChanged()
    }

    private val TAG: String = "MetroLinesAdapter"

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ItemHolder {
//        val binding = ItemMetroLineBinding.inflate()
        val item = DataBindingUtil.inflate<ItemMetroLineBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_metro_line, parent, false
        )
        return ItemHolder(item)
    }

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {
        holder.binding.root.setOnClickListener {
            val bundle = Bundle()
            bundle.putInt("id", list[position].lineId)
            bundle.putString("lineName",list[position].lineName)
            bundle.putString("nowLocation",mNowLocation)
            Navigation.findNavController(it)
                .navigate(R.id.action_metroHomeFragment_to_metroHomeLineDetailFragment,bundle)
        }
        holder.binding.line = list[position]
    }

    override fun getItemCount() = list.size

    class ItemHolder(itemMetroLineBinding: ItemMetroLineBinding) :
        RecyclerView.ViewHolder(itemMetroLineBinding.root) {
        var binding = itemMetroLineBinding
    }
}